﻿#ifndef _SCHEDULEDTASKS_H_INCLUDE_
#define _SCHEDULEDTASKS_H_INCLUDE_

#include <QObject>
#include <QTimer>
#include <QVector>

struct tagTaskRule
{
    tagTaskRule():runCount(1){}
    tagTaskRule(QVector<qint32> d,
                QVector<qint32> dow,
                QVector<qint32> h,
                QVector<qint32> m,
                QVector<qint32> s)
        : date(d),dayOfWeek(dow),
          hour(h),minute(m),second(s),
          runCount(1){}

    inline bool isNull(void)
    {
        if(date.isEmpty() &&
           dayOfWeek.isEmpty() &&
           hour.isEmpty() &&
           minute.isEmpty() &&
           second.isEmpty())
            return true;

        return false;
    }

    QVector<qint32> date;          // [1];//每月1号
    QVector<qint32> dayOfWeek;     // [1,3,5];每周一、周三、周五
    QVector<qint32> hour;          // [0,12]; 每天0点和12点开始推送
    QVector<qint32> minute;        // [0,5,10,15,20,25,30,35,40,45,50,55]; // 每隔 5 分钟执行一次
    QVector<qint32> second;        // [0,5,10,15]; 每分钟的0秒执行
    int runCount;                  // 任务执行次数
};

class Scheduledtasks : public QObject
{
    Q_OBJECT
public:
    Scheduledtasks(QObject *parent=nullptr);
    ~Scheduledtasks(void);

    /// 设置规则2
    void setRule2(QString date,QString dayOfWeek,
                  QString hour,QString minute,
                  QString second);
    /// 得到规则
    inline tagTaskRule& getRule(void) { return m_TaskRule; }
    /// 开始任务
    void startTask(void);
    /// 结束任务
    void stopTask(void);
    /// 检查任务是否在执行中
    inline bool isRunning(void) { return m_taskTimer.isActive(); }

signals:
    /// 执行任务
    void signal_timesatisfied(void);

private:
    bool getSplitString(QString srcString,QString startstr,QString endstr,QVector<qint32> &decNum);
    /// 设置规则
    inline void setRule(tagTaskRule rule) { m_TaskRule = rule; }

private slots:
    void onProcess_Scheduledtasks();

private:
    QTimer m_taskTimer;
    tagTaskRule m_TaskRule;
    int m_currentRunCount;
    QString m_CurrentDateTimeStr;
};

#endif // SCHEDULEDTASKS_H
